<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateConfigAPIRequest;
use App\Http\Requests\API\UpdateConfigAPIRequest;
use App\Models\Config;
use App\Models\Goods;
use App\Repositories\ConfigRepository;
use App\ZL\Controllers\ApiBaseController;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class ConfigController
 * @package App\Http\Controllers\API
 */

class ConfigAPIController extends ApiBaseController
{
    /** @var  ConfigRepository */
    private $configRepository;
    private $shop_id = null;

    protected $model_name = 'App\Models\Config';
    protected $model_load_array = [
    ];

    public function __construct(ConfigRepository $configRepo)
    {
        $this->configRepository = $configRepo;

        $this->shop_id = \request('shop_id',null);
    }

    public function authLimit()
    {
        $limit = [];
        $limit[] =  ['shop_id', $this->shop_id];
        return $limit;
    }

    public function getHotKeyword()
    {
        $config = Config::where('type','hotKeyword')->where('shop_id',$this->shop_id)->first();
        return responseZK(1,explode(',',str_replace('，',',', $config->value)),'成功');
    }

    public function setIndexLayout()
    {
        $config = Config::where('type','index_layout')->where('shop_id',$this->shop_id)->first();
        $json = \request('json');
        $other = [];

        while (true){
            $len = strlen($json);
            if($len>60000){
                $_other = substr($json,count($other)*60000,60000);
                $json = substr($json,60000);
                $other[] = $_other;
            }else{
                break;
            }
        }

        Config::where('type', 'like', "index_layout_%")->where('shop_id',$this->shop_id)->delete();
        if(count($other)>0) {
            foreach ($other as $k => $v) {
                Config::create([
                    'group' => 'layout',
                    'shop_id' => $this->shop_id,
                    'type' => 'index_layout_' . $k,
                    'value' => $v,
                    'title' => '布局设置副本',
                ]);
            }
        }
        $config->value = $json;
        $config->save();
        return responseZK(1,$config,'成功');
    }

    public function setIndexLayoutTest()
    {
        $config = Config::where('type','index_layout_test')->where('shop_id',$this->shop_id)->first();
        $json = \request('json');
        $other = [];

        while (true){
            $len = strlen($json);
            if($len>60000){
                $_other = substr($json,count($other)*60000,60000);
                $json = substr($json,60000);
                $other[] = $_other;
            }else{
                break;
            }
        }

        Config::where('type', 'like', "index_layout_test_%")->where('shop_id',$this->shop_id)->forceDelete();
        if(count($other)>0) {
            foreach ($other as $k => $v) {
                Config::create([
                    'group' => 'layout',
                    'type' => 'index_layout_test_' . $k,
                    'value' => $v,
                    'title' => '布局设置副本',
                ]);
            }
        }
        $config->value = $json;
        $config->save();
        return responseZK(1,$config,'成功');
    }

    public function getShippingArea()
    {
        $config = Config::where('type','shipping_area')->where('shop_id',$this->shop_id)->first();
        return responseZK(1,explode(',',str_replace('，',',', $config->value)),'成功');
    }

    public function getConfigsByGroup($group='system')
    {
        $goods_arr = Goods::with('attributesByStatus')->get()->keyBy('id')->toArray();
        $data = $this->configRepository->getMapByGroup($group, $this->shop_id);
        if(isset($data['index_layout'])){
            $other = Config::where('type', 'like', "index_layout_%")->where('shop_id',$this->shop_id)->get();
            $other_str = '';
            foreach ($other as $item) {
                $other_str.=$item->value;
            }
            $data['index_layout'] = json_decode($other_str.$data['index_layout'],true);
            foreach ($data['index_layout'] as &$v) {
//                foreach ($v['list'] as &$vv) {
//                    if(isset($vv['good']) && isset($goods_arr[$vv['good']])){
//                        $goods_arr[$vv['good']]['attributes'] = $goods_arr[$vv['good']]['attributes_by_status'];
//                        $__ar = $goods_arr[$vv['good']];
//                        unset($__ar['attributes_by_status']);
//                        $vv['goods'] = $__ar;
//                    }
//                }
            }
        }
        if(isset($data['index_layout_test']) && \request('is_test')){
            $other = Config::where('type', 'like', "index_layout_test_%")->where('shop_id',$this->shop_id)->get();
            $other_str = '';
            foreach ($other as $item) {
                $other_str.=$item->value;
            }
            $data['index_layout_test'] = json_decode($other_str.$data['index_layout_test'],true);
            foreach ($data['index_layout_test'] as &$v) {
//                foreach ($v['list'] as &$vv) {
//                    if(isset($vv['good']) && isset($goods_arr[$vv['good']])){
//                        $goods_arr[$vv['good']]['attributes'] = $goods_arr[$vv['good']]['attributes_by_status'];
//                        $__ar = $goods_arr[$vv['good']];
//                        unset($__ar['attributes_by_status']);
//                        $vv['goods'] = $__ar;
//                    }
//                }
            }
        }
        return responseZK(1,$data,'成功');
    }
}
